package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.PostShangXian;
import com.lc.afbx.util.DateTimePickDialogBefore;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PictureUtil;
import com.lc.afbx.util.Validator;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilLog;
import com.zcx.helper.util.UtilToast;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/5/23.
 */
public class BusinessInsuranceXubaoInsureActivity extends BaseActivity implements View.OnClickListener {


    @BoundView(R.id.yinlian_rl) private RelativeLayout yinlian_rl;
    @BoundView(R.id.goods_address_shangxian_rl) private RelativeLayout goods_address_shangxian_rl;
    private CheckBox dianZiQuan_img;
    private CheckBox zhiQuan_img;

    private TextView managerNum;
    private Bundle bundle;
    private TextView total;
    private TextView endTime;
    private TextView startTime;
    private TextView insurance_company;
    private TextView insurance_stores;
    private TextView insurance_type;
    private TextView insured;
    private TextView factory_plate_model;
    private EditText address;
    private EditText email;

    private ImageView imgYinLian;


    private int i;


    private PostShangXian postShangXian = new PostShangXian(new AsyCallBack<PostShangXian.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostShangXian.Info info) throws Exception {

            UtilToast.show(context, "投保成功");

            Intent intent = new Intent(context, PayWebActivity.class);
            intent.putExtra("ordersn", info.ordersn);
            intent.putExtra("type", "1");

            startActivity(intent);

            BaseApplication.INSTANCE.finishActivity(BusinessXubaoFillInfoActivity.class, BusinessXubaoPriceActivity.class, BusinessInsuranceXubaoInsureActivity.class);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouye_shangyexian_xubao_querendingdan);

        bundle = getIntent().getExtras();

        initView();

        setListener();

        address.setText(bundle.getString("dispatching", ""));
        email.setText(bundle.getString("email", ""));
        managerNum.setText(bundle.getString("manager_code", ""));

        String policy_type = bundle.getString("policy_type");

        if (policy_type.length() > 1) {

            dianZiQuan_img.setChecked(true);
            zhiQuan_img.setChecked(true);

        } else if (policy_type.equals("1")) {

            dianZiQuan_img.setChecked(true);


        } else if (policy_type.equals("2")) {

            zhiQuan_img.setChecked(true);
            goods_address_shangxian_rl.setVisibility(View.VISIBLE);

        }

    }

    private void setListener() {
        startTime.setOnClickListener(this);
        imgYinLian.setOnClickListener(this);
        yinlian_rl.setOnClickListener(this);

    }

    private void initView() {

        findViewById(R.id.back).setOnClickListener(this);
        findViewById(R.id.shouye_btn_jiaoqiangxian_querentoubao_toubao).setOnClickListener(this);

//        findViewById(R.id.rLayout_dianzi).setOnClickListener(this);
        findViewById(R.id.rLayout_zhi).setOnClickListener(this);

        address = ((EditText) findViewById(R.id.goods_address_qiangxian));
        email = ((EditText) findViewById(R.id.email_qiangxian));
        managerNum = ((TextView) findViewById(R.id.manager_number));
        //
        total = ((TextView) findViewById(R.id.total_querenbaodan));
        endTime = ((TextView) findViewById(R.id.endTime));
        startTime = ((TextView) findViewById(R.id.startTime));
        insurance_company = ((TextView) findViewById(R.id.insurance_company));
        insurance_stores = ((TextView) findViewById(R.id.insurance_stores));
        insurance_type = ((TextView) findViewById(R.id.insurance_type));
        insured = ((TextView) findViewById(R.id.insured));
        factory_plate_model = ((TextView) findViewById(R.id.factory_plate_model));
//        支付的图片  圆点
        imgYinLian = ((ImageView) findViewById(R.id.img_yinlian_shangyexian));
        imgYinLian.setTag(true);

//        new DecimalFormat(".00").format(bundle.getString("heji", "这个接口 不能接"))
//        total.setText(bundle.getString("heji", "这个接口 不能接"));
        total.setText(bundle.getString("totalMoney") + "元");
        insurance_company.setText(bundle.getString("company", "=========="));
        insurance_stores.setText(bundle.getString("shopName", "=========="));
        insured.setText(bundle.getString("name", "============="));
        factory_plate_model.setText(bundle.getString("brand_model", "============="));

        if (bundle.getString("insurance_type").equals("1")) {
            insurance_type.setText("新保");
            insurance_type.setTextColor(getResources().getColor(R.color.xinbao));
        } else {
            insurance_type.setText("续保");
            insurance_type.setTextColor(getResources().getColor(R.color.xubao));
        }

        //这部分是电子保单  纸质保单
        dianZiQuan_img = ((CheckBox) findViewById(R.id.quanquan1));
        zhiQuan_img = ((CheckBox) findViewById(R.id.quanquan2));
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.startTime:
                DateTimePickDialogBefore dialogBefore = new DateTimePickDialogBefore(context, endTime);
                dialogBefore.dateTimePicKDialog(startTime);
                return;

            case R.id.yinlian_rl:

//                if (i++ % 2 == 0) {
//                    imgYinLian.setImageResource(R.mipmap.xubaovb);
//                    imgYinLian.setTag(true);
//                } else {
//                    imgYinLian.setImageResource(R.mipmap.xb);
//                    imgYinLian.setTag(false);
//                }
                return;

            case R.id.rLayout_dianzi:

//                if (!dianZiQuan_img.isChecked()) {
//
//                    dianZiQuan_img.setChecked(true);
//
//                } else {
//
//                    dianZiQuan_img.setChecked(false);
//
//                }

                return;

            case R.id.rLayout_zhi:

                if (!zhiQuan_img.isChecked()) {

                    zhiQuan_img.setChecked(true);

                    goods_address_shangxian_rl.setVisibility(View.VISIBLE);

                } else {

                    zhiQuan_img.setChecked(false);

                    goods_address_shangxian_rl.setVisibility(View.GONE);
                    address.setText("");

                }

                return;

            case R.id.shouye_btn_jiaoqiangxian_querentoubao_toubao:

                if (TextUtils.isEmpty(startTime.getText().toString())) {
                    UtilToast.show(this, "注意起保时间 不能为空");
                    return;
                }

                if (dianZiQuan_img.isChecked() && zhiQuan_img.isChecked()) {

                    postShangXian.policy_type = "1,2";

                } else if (dianZiQuan_img.isChecked()) {
                    postShangXian.policy_type = "1";
                } else if (zhiQuan_img.isChecked()) {
                    postShangXian.policy_type = "2";
                } else {

                    MyToast.showToast("请选择保单形式", context);
                    return;
                }

                if (TextUtils.isEmpty(email.getText().toString())) {
                    UtilToast.show(this, "注意邮箱 不能为空");
                    return;
                }
                if (zhiQuan_img.isChecked() && TextUtils.isEmpty(address.getText().toString().replaceAll(" ", ""))) {
                    UtilToast.show(this, "注意地址 不能为空");
                    return;
                }
                if (((boolean) imgYinLian.getTag()) == false) {
                    MyToast.showToast("请选择 支付方式", context);
                    return;
                }

                if (!Validator.isEmail(email.getText().toString())) {
                    UtilToast.show(this, "邮箱格式 不正确");
                    return;
                }


                JSONObject jsonObject = null;      //被保人信息
                JSONObject jsonObject1 = null;     //背包车辆信息
                try {

                    jsonObject = new JSONObject(bundle.getString("insurant_info"));

                    jsonObject1 = new JSONObject(bundle.getString("vehicle_info"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                UtilLog.e("===========================3", bundle.getString("insurance_type") + "sdfasdfsaasdfas");
                UtilLog.e("===========================3", jsonObject.toString());

                postShangXian.uid = BaseApplication.BasePreferences.readUserId();//用户id
                postShangXian.applicant = bundle.getString("applicant");//投保人
                postShangXian.insurant_info = jsonObject.toString();
                postShangXian.vehicle_info = jsonObject1.toString();
                postShangXian.business_risk = "";          //TODO  商险险种选择
                postShangXian.insurance_type = bundle.getString("insurance_type");//投保类型
                postShangXian.covered_area = BaseApplication.BasePreferences.readCityID();//投保地区  码
                postShangXian.insurance_id = bundle.getString("cid");   //投保公司 id
                postShangXian.stores_id = bundle.getString("shopId");   //投保门店id
                postShangXian.total = bundle.getString("totalMoney");

                postShangXian.id_url_old = bundle.getString("id_url_old", "");
                postShangXian.drive_url_old = bundle.getString("drive_url_old", "");
                postShangXian.people_car_url_old = bundle.getString("people_car_url_old", "");

                postShangXian.starttime = startTime.getText().toString();
                postShangXian.endtime = endTime.getText().toString();
                postShangXian.link_ordersn = bundle.getString("ordersn", "");

                postShangXian.id_url = "";
                postShangXian.drive_url = "";
                postShangXian.people_car_url = "";


                if (dianZiQuan_img.isChecked() && zhiQuan_img.isChecked()) {

                    postShangXian.policy_type = "1,2";

                } else if (dianZiQuan_img.isChecked()) {
                    postShangXian.policy_type = "1";
                } else if (zhiQuan_img.isChecked()) {
                    postShangXian.policy_type = "2";
                }

                postShangXian.email = email.getText().toString();                //邮箱
                postShangXian.dispatching = address.getText().toString();       //地址
                postShangXian.manager_code = managerNum.getText().toString();//客户经理编码

                postShangXian.execute(this);

                break;
        }


    }


}
